package com.yanqu.road.server.logic.activity.condition.recharge;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UserActivityConditionData;
import com.yanqu.road.entity.config.goods.GetRechargeMallResult;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.recharge.RechargeTriggerArgs;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.ActivityModule;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.logic.activity.condition.BaseActivityCondition;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.List;

/**
 * 现金礼包，充值条件触发
 */
public class RechargeChargeActivityCondition extends BaseActivityCondition {

	public RechargeChargeActivityCondition(ActivityInfo activityInfo, ActivityConditionInfo activityConditionInfo, BaseActivityData baseActivityData) {
		super(activityInfo, activityConditionInfo, baseActivityData);
	}

	@Override
	public void addTrigger(GamePlayer player) {
		player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.RechargeTrigger.getValue(), handle);
	}

	@Override
	public void removeTrigger(GamePlayer player) {
		player.getModule(TaskModule.class).getGameEvent().removeListener(eGamePlayerEventType.RechargeTrigger.getValue(), handle);
	}

	public boolean isCompleted(GamePlayer player) {
		return getValue().compareTo(BigInteger.ZERO) > 0;
	}

	@Override
	protected void player_BoardLevelHandle(Object object) {
		int param4 = activityConditionInfo.getParamList().get(3).intValue();  //开服天数
		int param5 = activityConditionInfo.getParamList().get(4).intValue();  //身份
		int param6 = activityConditionInfo.getParamList().get(5).intValue();  //充值次数
		int param7 = activityConditionInfo.getParamList().get(6).intValue();  //获得vip
		int param9 = activityConditionInfo.getParamList().get(8).intValue();  //富甲一方的活动条件类型 1为富甲一方 2为富可敌国 1未触发以及触发未结束时无法触发2

		int openServerDay = ServerListMgr.getServerOpenDay(GameServer.getInstance().getServerId());
		int title = player.getTitleId();

		long triggerTime = 0;
		long rechargeTimes = 0;
		long addVipExp = 0;
		if(object instanceof RechargeTriggerArgs){
			if (getValue().compareTo(BigInteger.ZERO) > 0) {
				return;
			}
			RechargeTriggerArgs triggerArgs = (RechargeTriggerArgs) object;
			String param = getParam();
			if(!StringUtils.isNullOrEmpty(param)){
				String[] paramArr = param.split("\\|");
				triggerTime = Long.valueOf(paramArr[0]);
				rechargeTimes = Long.valueOf(paramArr[1]);
				addVipExp = Long.valueOf(paramArr[2]);
			}
			rechargeTimes++;
			addVipExp += triggerArgs.getAddVip();

			setParam(triggerTime + "|" + rechargeTimes + "|" + addVipExp);

//			if(openServerDay < param4){//开服天数
//				return;
//			}
//			if(title < param5){//身份条件
//				return;
//			}
//			if(rechargeTimes < param6){
//				return;
//			}
//			if(addVipExp < param7){
//				return;
//			}
//			if (getValue().compareTo(BigInteger.ZERO) == 0) {
//				GetRechargeMallResult result = player.getModule(ActivityModule.class).getShowRMBMallInfo(activityConditionInfo);
//				if (result.isDuringCd()) {
//					return;
//				}
//				setRMBMallActivityInfo(result.getMallInfo());
//			}
		}else {
			int args = (int)object;
			if(args > 0){
				//加道具，设置购买状态
				if(getValue().intValue() == args) {
					getRMBMallActivityReward(args);
				}
			}else {
				if (getValue().compareTo(BigInteger.ZERO) > 0) {
					return;
				}
				if(openServerDay < param4){//开服天数
					return;
				}
				if(title < param5){//身份条件
					return;
				}
				if (param9 == 2){
					boolean canTriggerType2RMBMall = checkCanTriggerType2RMBMall();
					//富可敌国的活动条件类型2， 类型1未触发以及触发未结束时无法触发
					if (!canTriggerType2RMBMall){
						return;
					}
				}
				String param = getParam();
				if(!StringUtils.isNullOrEmpty(param)){
					String[] paramArr = param.split("\\|");
					rechargeTimes = Long.valueOf(paramArr[1]);
					addVipExp = Long.valueOf(paramArr[2]);
				}

				if(rechargeTimes < param6){
					return;
				}
				if(addVipExp < param7){
					return;
				}
				if (getValue().compareTo(BigInteger.ZERO) == 0) {
					GetRechargeMallResult result = player.getModule(ActivityModule.class).getShowRMBMallInfo(activityConditionInfo);
					if (result.isDuringCd()) {
						return;
					}
					setRMBMallActivityInfo(result.getMallInfo());
				}
			}
		}
	}

	/**
	 * 检查是否可以触发富可敌国的活动条件类型2
	 * @return
	 */
	
	public boolean checkCanTriggerType2RMBMall(){
		List<BaseActivityCondition> baseActivityConditionList = baseActivityData.getBaseActivityConditionList();
		boolean isCanTrigger = true;
		for (BaseActivityCondition conditionData: baseActivityConditionList) {
			int param9 = conditionData.getActivityConditionInfo().getParamList().get(8).intValue();
			if (param9 == 2){
				continue;
			}
			//还有没触发过的低级礼包 不能触发
			if (conditionData.getValue().compareTo(BigInteger.ZERO) <= 0){
				isCanTrigger = false;
				break;
			}
			//如果已经触发过 param就是结束时间 判断当前是否超过结束时间
			UserActivityConditionData userActivityConditionData = baseActivityData.getUserActivityConditionData(conditionData.getConditionId());
			if (userActivityConditionData == null){
				isCanTrigger = false;
				break;
			}
			//已经触发过的param不会带有|分隔符 未触发过的param会带有|分隔符
			String param = userActivityConditionData.getParam();
			//先转换成结束时间
			long endTime = StringUtils.string2longSafe(param, 0);
			if (endTime <= 0){
				isCanTrigger = false;
				break;
			}
			//判断当前时间是否超过结束时间 如果没结束 就不能触发
			if (System.currentTimeMillis() < endTime*1000) {
				isCanTrigger = false;
				break;
			}

		}
		return isCanTrigger;
	}
}
